/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#include <google/protobuf/io/coded_stream.h>
#include <kmhdfs/rpc/rpc_content_wrapper.h>

using namespace ::google::protobuf;
using namespace ::google::protobuf::io;

namespace Hdfs {
namespace Internal {

RpcContentWrapper::RpcContentWrapper(Message * header, Message * msg) :
    header(header), msg(msg) {
}

int RpcContentWrapper::getLength() {
    int headerLen, msgLen = 0;
    headerLen = header->ByteSizeLong();
    msgLen = msg == nullptr ? 0 : msg->ByteSizeLong();
    return headerLen + CodedOutputStream::VarintSize32(headerLen)
           + (msg == nullptr ?
              0 : msgLen + CodedOutputStream::VarintSize32(msgLen));
}

void RpcContentWrapper::writeTo(WriteBuffer & buffer) {
    int size = header->ByteSizeLong();
    buffer.writeVarint32(size);
    header->SerializeToArray(buffer.alloc(size), size);

    if (msg != nullptr) {
        size = msg->ByteSizeLong();
        buffer.writeVarint32(size);
        msg->SerializeToArray(buffer.alloc(size), size);
    }
}

}
}

